# Copyright 2020-2022 Fugue, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
package rules.tf_google_sql_database_pg_enable_log_min_messages

import data.fugue
import data.google.sql_database.sql_database_library as lib

__rego__metadoc__ := {
  "custom": {
    "controls": {
      "CIS-Google_v1.1.0": [
        "CIS-Google_v1.1.0_6.2.5"
      ],
      "CIS-Google_v1.2.0": [
        "CIS-Google_v1.2.0_6.2.13"
      ]
    },
    "severity": "Medium"
  },
  "description": "PostgreSQL database instance 'log_min_error_statement' database flag should be set appropriately. The PostgreSQL database instance flag 'log_min_messages' controls which message levels are written to the server log. Valid values include INFO, WARNING, and ERROR. Each level includes all the levels that follow it. The default is WARNING. If this flag is not set to the correct value, important messages useful for troubleshooting may not be logged.",
  "id": "FG_R00428",
  "title": "PostgreSQL database instance 'log_min_error_statement' database flag should be set appropriately"
}

resource_type := "MULTIPLE"

valid_db_instances[id] {
  db = lib.postgres_database_instances[id]
  flag := lib.get_db_flag_with_default(db, "log_min_messages", "error")
  flag != "panic"
}

policy[j] {
  db = lib.postgres_database_instances[id]
  valid_db_instances[id]
  j = fugue.allow_resource(db)
}

policy[j] {
  db = lib.postgres_database_instances[id]
  not valid_db_instances[id]
  j = fugue.deny_resource(db)
}
